<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Validator\OrderValidator;
use App\Service\OrderService;
use App\Service\DataReturnService as DRS;

class OrderCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:orderCancel';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Cancel orders after 5 minutes';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $orders = DB::table('orders')
                    ->where(['type'=> 3, 'payment_status' => 0, 'status' =>1])
                    ->get()
                    ->toarray();

        foreach ($orders as $key => $order) {
            if(strtotime($order->createtime)+Config('app.orderExpireTime') < time() )
            {
                // 取消订单
                $result = OrderService::removeOrder($order->batchcode, $order->user_id);
                if($result['code'] != 666){
                    var_dump($result);
                    echo date('Y-m-d H:i:s').':::---------------Failed Order Id :'.$order->id.'---------------';
                }else{
                    echo date('Y-m-d H:i:s').':::---------------OK--------------';
                }
                
            }
        }
    }

    // //取消订单
    // public function removeOrder($batchcode, $user_id)
    // {
    //     $order = OrderValidator::validateRemoveOrder($batchcode, $user_id);
    //     if($order['code'] != 666)
    //         return $order;

    //     $orderInfo = OrderValidator::validateRemoveOrderInfo($batchcode, $user_id);
    //     if($orderInfo['code'] != 666)
    //         return $orderInfo;

    //     DB::beginTransaction();
    //     try{
    //         // 更改订单状态为关闭交易
    //         DB::table('orders')
    //             ->where(['batchcode' => $batchcode, 'user_id'=> $user_id])
    //             ->update(['status' => 7, 'last_time' => date('Y-m-d H:i:s')]);

    //         // 更改订单详情的订单状态为关闭交易
    //         DB::table('orders_info')
    //             ->where(['batchcode' => $batchcode, 'user_id'=> $user_id])
    //             ->update(['status' => 7,  'last_time' => date('Y-m-d H:i:s')]);

    //         // 更改商品状态为四种对应的未售
    //         foreach ($orderInfo['data'] as $oinfo) 
    //         { 
    //             if($oinfo['product_state'] == 5){
    //                 DB::table('product')
    //                     ->where(['id' => $oinfo['product_id']])
    //                     ->update(['product_state' => 1, 'updatetime' =>date('Y-m-d H:i:s')]);
    //             }
    //             elseif($oinfo['product_state'] == 6 ){
    //                 DB::table('product')
    //                     ->where(['id' => $oinfo['product_id']])
    //                     ->update(['product_state' => 2, 'updatetime' =>date('Y-m-d H:i:s')]);
    //             }elseif($oinfo['product_state'] == 7 ){
    //                 DB::table('product')
    //                     ->where(['id' => $oinfo['product_id']])
    //                     ->update(['product_state' => 3, 'updatetime' =>date('Y-m-d H:i:s')]);
    //             }elseif($oinfo['product_state'] == 8 ){
    //                 DB::table('product')
    //                     ->where(['id' => $oinfo['product_id']])
    //                     ->update(['product_state' => 4, 'updatetime' =>date('Y-m-d H:i:s')]);
    //             }
    //         }

    //         DB::commit();
    //         return DRS::returnOk(true);
    //     }catch(\Exception $exception) {
    //         DB::rollBack();
    //         return DRS::returnError(1000,'取消订单出错，稍后再试！',$exception);
    //     }
    // }
}
